package com.zstu.mall.service.impl;

import com.zstu.mall.entity.Product;
import com.zstu.mall.entity.ProductActivity;
import com.zstu.mall.entity.User;
import com.zstu.mall.entity.UserOrder;
import com.zstu.mall.service.ProductService;
import com.zstu.mall.service.ShopCartService;
import com.zstu.mall.service.UserOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.awt.*;
import java.util.*;
import java.util.List;

@Service
public class ShopCartServiceImpl implements ShopCartService {

    @Autowired
    private ProductService productService;

    @Autowired
    private UserOrderService userOrderService;

    /**
     * 加购物车
     *
     * @param productId
     * @param request
     */
    @Override
    public void addCart(String productId, HttpServletRequest request) throws Exception {
        User loginUser = (User) request.getSession().getAttribute("user");
        if (loginUser == null)
            throw new Exception("未登录！请重新登录");
        Product product = productService.get(productId);
        boolean existThisProduct = false;
        double total = 0.0;
        double discount = 1.0;
        //如果购物车存在该商品+1
        List<UserOrder> orderItems = userOrderService.findByOrderId(NAME_PREFIX + loginUser.getId());
        for(UserOrder userOrder : orderItems){
            if(userOrder.getProductId().equals(productId) && userOrder.getStatus().equals("0")){
                userOrder.setNumber(userOrder.getNumber()+1);
                total = product.getPrice()*userOrder.getNumber();
                if(!product.getActivityId().equals(" "))
                    discount = product.getProductActivitys().get(0).getDiscount();
                //折后
                userOrder.setTotal(total*discount);
                //原价
                userOrder.setString1(String.valueOf(total));
                existThisProduct = true;
                userOrderService.update(userOrder);
                break;
            }
        }
        //如果不存在
        if(!existThisProduct){
            UserOrder userOrder = new UserOrder();
            userOrder.setId(userOrderService.createId());
            userOrder.setProductId(productId);
            userOrder.setOrderId(NAME_PREFIX + loginUser.getId());
            if(!product.getActivityId().equals(" "))
                discount = product.getProductActivitys().get(0).getDiscount();
            userOrder.setTotal(product.getPrice()*discount);
            userOrder.setString1(String.valueOf(product.getPrice()));
            userOrderService.create(userOrder);
        }
    }

    /**
     * 移除
     *
     *
     * @param id
     * @param request
     */
    @Override
    public void remove(String id, HttpServletRequest request) throws Exception {
        User loginUser = (User) request.getSession().getAttribute("user");
        if (loginUser == null)
            throw new Exception("未登录！请重新登录");
        userOrderService.delete(id);
    }

    /**
     * 查看购物车
     *
     * 查询出session的List中所有的商品Id,并封装成List<OrderItem>返回
     *
     * @param request
     * @return
     */
    @Override
    public List<UserOrder> listCart(HttpServletRequest request) throws Exception {
        User loginUser = (User) request.getSession().getAttribute("user");
        if (loginUser == null)
            throw new Exception("未登录！请重新登录");
        List<UserOrder> orderItems = userOrderService.findByOrderId(NAME_PREFIX + loginUser.getId());
        for(UserOrder userOrder : orderItems){
            Product product = productService.get(userOrder.getProductId());
            System.out.println(product.toString());
            userOrder.setProduct(product);
            //移除非购物车状态
            if(!userOrder.getStatus().equals("0"))
                orderItems.remove(userOrder);
        }
        System.out.println("listcart over");
        return orderItems;
    }

    /**
     * 修改
     *
     * 修改session List中对应商品Id的number
     *
     * @param id,number
     * @param request
     */
    @Override
    public void updateCart(String id, Double number, HttpServletRequest request) throws Exception {
        User loginUser = (User) request.getSession().getAttribute("user");
        if (loginUser == null)
            throw new Exception("未登录！请重新登录");
        UserOrder userOrder = userOrderService.get(id);
        Product product = productService.get(userOrder.getProductId());
        userOrder.setNumber(number);
        double discount = 1.0;
        double total = product.getPrice()*userOrder.getNumber();
        if(!product.getActivityId().equals(" "))
            discount = product.getProductActivitys().get(0).getDiscount();
        //折后
        userOrder.setTotal(total*discount);
        //原价
        userOrder.setString1(String.valueOf(total));
        userOrderService.update(userOrder);
    }

}